var o = require("./config").openIdUrl;
// app.js
App({
  data: {
    loginurl: require("./config").openIdUrl,
    host: require("./config").host,
  },
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  globalData: {
    userInfo: null,
    openid: ""
  },
  getUserInfo: function(e) {
    var a = this;
    "" !== a.globalData.openid ? e(null, a.globalData.openid) : wx.login({
        success: function(l) {
            wx.setStorageSync("code", l.code), wx.getUserInfo({
                success: function(c) {
                    a.globalData.userInfo = c.userInfo, "function" == typeof cb && cb(a.globalData.userInfo),
                    wx.setStorageSync("userinfo", c.userInfo), wx.request({
                        method:"GET",
                        url: o + "?code=" + l.code,
                        data: {
                            nickname: c.userInfo.nickName,
                            gender: c.userInfo.gender,
                            city: c.userInfo.city,
                            province: c.userInfo.province,
                            country: c.userInfo.country,
                            avatarUrl: c.userInfo.avatarUrl
                        },
                        success: function(o) {
                            a.globalData.openid = o.data.openid, wx.setStorageSync("openid", o.data.openid),
                            wx.setStorageSync("userid", o.data.did);
                            var l = o.data.session_key;
                            wx.setStorageSync("sessionId", l), e(null, a.globalData.openid);
                        },
                        fail: function(o) {
                            console.log("拉取用户openid失败，将无法正常使用开放接口等服务", o), e(o);
                        }
                    });
                },
                fail: function(o) {
                    wx.hideToast(), wx.showModal({
                        title: "提示",
                        content: "登录失败,请允许小程序获取用户信息",
                        showCancel: !1,
                        success: function(o) {
                            o.confirm && wx.openSetting({
                                success: function(o) {
                                    o.authSetting["scope.userInfo"] ? a.checkLogin() : wx.navigateBack({
                                        delta: 1
                                    });
                                }
                            });
                        }
                    });
                }
            });
        },
        fail: function(o) {
            wx.showModal({
                title: "提示",
                content: "登录失败,请稍候再试",
                showCancel: !1,
                success: function(o) {
                    o.confirm && wx.navigateBack({
                        delta: 1
                    });
                }
            }), e(o);
        }
    });
},
checkLogin: function(o) {
    wx.showModal({
        title: "提示",
        content: "您尚未登录,点击确定一键登录",
        success: function(e) {
            e.confirm ? wx.navigateTo({
                url: "../login/login"
            }) : 1 == o || wx.navigateBack();
        }
    });
},
dologin: function() {
    var o = this;
    wx.showToast({
        title: "登录中...",
        icon: "loading",
        duration: 8e3
    }), o.getUserInfo(function(o) {
        console.log(o), wx.hideToast(), null != o ? wx.showModal({
            title: "提示",
            content: "登录失败",
            showCancel: !1,
            duration: 3e3
        }) : wx.showModal({
            title: "提示",
            content: "登录成功",
            showCancel: !1,
            success: function(o) {
                o.confirm && wx.navigateBack();
            }
        });
    });
}
})
